using System;
using System.IO;
using System.Threading;
using NUnit.Framework;

namespace Framework.Testing
{
    public class TestDataBase : IDisposable
    {
        private bool _disposed;

        public TestDataBase()
        {
        }

        #region Disposing

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }


        private void Dispose(bool disposing)
        {
            if (this._disposed)
                return;

            if (disposing)
            {
            }

            _disposed = true;
        }

        ~TestDataBase()
        {
            Dispose(false);
        }

        #endregion
    }

    public abstract class TestFixtureBase<TData>  where TData : class, IDisposable
    {
        private TData _data;

        public TData Data
        {
            get { return _data; }
            set { _data = value; }
        }

        [TearDown]
        public void Teardown()
        {
            if (_data != null)
            {
                _data.Dispose();
                _data = null;
            }
        }

        protected T OnNewThread<T>(Func<T> func)
        {
            var res = default(T);
            var t = new Thread(() => res = func());
            t.Start();
            t.Join();
            return res;
        }

        protected static bool FileExist(string relativePath)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory;
            var fullName = Path.Combine(path, relativePath);
            return File.Exists(fullName);
        }
    }
}